package com.hm.health.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.goodix.utils.DensityUtil;
import com.goodix.utils.TimeUtil;
import com.hm.health.Activity.ecgflash.EcgFlashDetailActivity;
import com.hm.health.R;
import com.hm.health.Utils.CommonUtils;
import com.hm.health.entity.EcgFlashRecordBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EcgFlashSummaryAdapter extends RecyclerView.Adapter<EcgFlashSummaryAdapter.ViewHolder> {
    public static final String TAG = "EcgFlashSummaryAdapter";
    private List<EcgFlashRecordBean> mRecordBeanList;
    private Context mContext;
    private OnClickListener longClickListener;

    public EcgFlashSummaryAdapter(List<EcgFlashRecordBean> list, Context context) {
        mRecordBeanList = list;
        mContext = context;
    }

    public void setLongClickListener(OnClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_ecg_record, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (mRecordBeanList == null || mRecordBeanList.isEmpty() || mContext == null) {
            return;
        }
        EcgFlashRecordBean recordBean = mRecordBeanList.get(position);
        if (recordBean == null) {
            return;
        }
        holder.mLinearLayoutDaily.removeAllViews();

        int record_count = recordBean.getRecordNum();
        for (int i=0; i<record_count; i++) {
            TextView tv = new TextView(mContext);

            EcgFlashRecordBean.CsvFile csvFile = recordBean.getRecord(i);
            String startTime = TimeUtil.formateEcgSecond(csvFile.startTimeStamp);
            String endTime = TimeUtil.formateEcgSecond(csvFile.endTimeStamp);

            tv.setText(startTime + "-" + endTime);
            tv.setBackground(mContext.getResources().getDrawable(R.drawable.home_btn_background));
            tv.setTextSize(16);
            tv.setHeight(100);
            tv.setTextColor(mContext.getResources().getColor(R.color.black));
            tv.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(holder.mLinearLayoutDaily.getLayoutParams());
            lp.setMargins(DensityUtil.dip2px(mContext, 16), 10, DensityUtil.dip2px(mContext, 16), 0);
            tv.setLayoutParams(lp);
            tv.setOnClickListener(view -> {
                // 判断csv是否需要pc仿真才能看到结果
                if (!csvFile.hasAlgoResult) {
                    Toast.makeText(mContext, mContext.getString(R.string.ecg_flash_need_pc_simu), Toast.LENGTH_SHORT).show();
                    return;
                }

                Intent intent = new Intent(mContext, EcgFlashDetailActivity.class);
                intent.putExtra(EcgFlashDetailActivity.EXT_ECG_PATCH_TEST_RECORD_ID, recordBean.getTestRecordId());
                intent.putExtra(EcgFlashDetailActivity.EXT_ECG_PATCH_CSV_ID, csvFile.csvId);
                intent.putExtra(EcgFlashDetailActivity.EXT_ECG_PATCH_CSV_TIME, csvFile.startTimeStamp);

                mContext.startActivity(intent);
            });
            holder.mLinearLayoutDaily.addView(tv);
        }

        holder.mTvRecordNum.setText(String.format(mContext.getString(R.string.ecg_flash_total_count_format), record_count));
        holder.mTvTitle.setText(recordBean.getTestRecordId());
        holder.mTvDate.setText(recordBean.getTesterName());
        /*holder.mLinearLayout.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, EcgFlashDailySummaryActivity.class);
            intent.putExtra(EcgFlashDailySummaryActivity.EXT_ECG_FLASH_RECORD_DATE, recordBean.getDayStr());
            mContext.startActivity(intent);
        });*/
        holder.mLinearLayoutGeneral.setOnClickListener(view -> {
            if (holder.mLinearLayoutDaily.getVisibility() == View.GONE) {
                CommonUtils.runOnUIThread(() -> {
                    holder.mLinearLayoutDaily.setVisibility(View.VISIBLE);
                    holder.mTvClick.setText(mContext.getString(R.string.ecg_flash_click_off));
                    holder.mTvClick.setTextColor(mContext.getResources().getColor(R.color.blue));
                });
            } else if (holder.mLinearLayoutDaily.getVisibility() == View.VISIBLE) {
                CommonUtils.runOnUIThread(() -> {
                    holder.mLinearLayoutDaily.setVisibility(View.GONE);
                    holder.mTvClick.setText(mContext.getString(R.string.ecg_flash_click));
                    holder.mTvClick.setTextColor(mContext.getResources().getColor(R.color.red));
                });
            }
        });
        CommonUtils.runOnUIThread(() -> {
            holder.mLinearLayoutDaily.setVisibility(View.GONE);
            holder.mTvClick.setText(mContext.getString(R.string.ecg_flash_click));
            holder.mTvClick.setTextColor(mContext.getResources().getColor(R.color.red));
        });

        holder.mTvTitle.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (longClickListener != null) {
                    longClickListener.longClick(holder, position);
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mRecordBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_ecg_flash_general)
        LinearLayout mLinearLayout;
        @BindView(R.id.ecg_flash_record_num)
        TextView mTvRecordNum;
        @BindView(R.id.item_ecg_flash_title)
        TextView mTvTitle;
        @BindView(R.id.item_ecg_flash_date)
        TextView mTvDate;
        @BindView(R.id.ecg_flash_record_linear)
        LinearLayout mLinearLayoutGeneral;
        @BindView(R.id.ecg_flash_record_click)
        TextView mTvClick;
        @BindView(R.id.ecg_flash_daily_linear)
        LinearLayout mLinearLayoutDaily;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnClickListener {
        void longClick(ViewHolder holder, int position);
    }
}
